home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / commands.c < prev    next >
C/C++ Source or Header  |  1994-11-24  |  6KB  |  261 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.00 -- module commands.c                     */
  5. /*                                         */
  6. /*    Most of the entries for commands assignable to keys             */
  7. /*    Written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    see COPYRIGHT.xsok for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #ifndef _POSIX_SOURCE
  14. #define _POSIX_SOURCE
  15. #endif
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include "xsok.h"
  19. #include "version.h"
  20.  
  21. void cmd_Up(void) {
  22.     playermove(0);
  23. }
  24. void cmd_Left(void) {
  25.     playermove(1);
  26. }
  27. void cmd_Down(void) {
  28.     playermove(2);
  29. }
  30. void cmd_Right(void) {
  31.     playermove(3);
  32. }
  33.  
  34. void cmd_Repeat(void) {
  35.     if (game.n_moves)
  36.     playermove(movetab[game.n_moves-1]);
  37. }
  38.  
  39. void cmd_ShowAuthor(void) {
  40.     if (*levelauthor)
  41.     show_message("%s", levelauthor);
  42.     else
  43.     show_message("%s", TXT_NOAUTHOR);
  44. }
  45.  
  46. void cmd_LevelInfo(void) {
  47.     if (*levelcomment) {
  48.     if (highscore[game.level])
  49.         show_message("%s (Best Score: %d)", levelcomment, highscore[game.level]);
  50.     else
  51.         show_message("%s (Unsolved level)", levelcomment);
  52.     } else
  53.     cmd_ShowBestScore();
  54. }
  55.  
  56. void cmd_NextUnsolved(void) {
  57.     while (game.level < maxlevel) {
  58.     if (!highscore[++game.level]) {
  59.         NewLevel(game.level);
  60.         cmd_LevelInfo();
  61.         return;
  62.     }
  63.     }
  64. }
  65.  
  66. void cmd_NextLevel(void) {
  67.     if (numeric_arg)
  68.     if (numeric_arg <= maxlevel)
  69.         game.level = numeric_arg - 1;    /* & fall through */
  70.     if (game.level < maxlevel) {
  71.     if (game.stored_moves && !game.finished) {
  72.         play_sound("giveup");
  73.     }
  74.     NewLevel(game.level+1);
  75.     cmd_LevelInfo();
  76.     }
  77. }
  78.  
  79. void cmd_PrevLevel(void) {
  80.     if (game.level > 1) {
  81.     if (game.stored_moves && !game.finished) {
  82.         play_sound("giveup");
  83.     }
  84.     NewLevel(game.level-1);
  85.     cmd_LevelInfo();
  86.     }
  87. }
  88.  
  89. void rq_LeaveSok(void) {
  90.     request_confirm(cmd_LeaveSok, TXT_QUIT_CONFIRM);
  91. }
  92.  
  93. /* unused, since this can be undone */
  94. void rq_RestartGame(void) {
  95.     request_confirm(cmd_RestartGame, TXT_RESTART_CONFIRM);
  96. }
  97.  
  98. void rq_PrevLevel(void) {
  99.     request_confirm(cmd_PrevLevel, TXT_PREV_CONFIRM);
  100. }
  101. void rq_NextLevel(void) {
  102.     request_confirm(cmd_NextLevel, TXT_NEXT_CONFIRM);
  103. }
  104. void rq_NextUnsolved(void) {
  105.     request_confirm(cmd_NextUnsolved, TXT_UNSOLVED_CONFIRM);
  106. }
  107.  
  108.  
  109. void cmd_DropBookmark(void) {
  110.     game.bookmark = game.n_moves;    /* easy, isn't it? */
  111.     show_message(TXT_BOOKMARK_SET);
  112. }
  113.  
  114. void jumpto_movenr(int move_ptr) {
  115.     int remgraphic = gamegraphic;
  116.     if (move_ptr == game.n_moves)
  117.     return;
  118.     /* assert(move_ptr <= game.stored_moves); */
  119.     if (remgraphic)        /* graphic was on */
  120.     graphics_control(Disable);
  121.  
  122.     if (move_ptr < game.n_moves)    /* must reset first */
  123.     OrgLevel();
  124.     while (move_ptr > game.n_moves) {
  125.     /* printf("doing move %d of %d (%d)\n", game.n_moves, move_ptr, movetab[game.n_moves]); */
  126.     int xx;
  127.     xx = game.n_moves;
  128.     playermove(movetab[game.n_moves]);
  129.     if (xx == game.n_moves)
  130.         fatal("Shit, same old bug again!\n");
  131.     }
  132.     if (remgraphic)
  133.     graphics_control(EnableAndRedraw);
  134. }
  135.  
  136. void cmd_RestartGame(void) {
  137.     if (numeric_arg <= game.stored_moves)
  138.     jumpto_movenr(numeric_arg);
  139.     else 
  140.     jumpto_movenr(game.stored_moves);
  141.     cmd_LevelInfo();
  142. }
  143.  
  144. void cmd_GotoBookmark(void) {
  145.     jumpto_movenr(game.bookmark);
  146.     cmd_ShowScore();
  147. }
  148.  
  149. void cmd_SaveGame(void) {
  150.     game.finished = compute_score();
  151.     save_game("sv");
  152. }
  153. void cmd_ShowVersion(void) {
  154.     show_message(TXT_VERSION, VERSION);
  155. }
  156.  
  157.  
  158. void cmd_ShowScore(void) {
  159.     compute_score();
  160.     show_message(TXT_SCORE, game.n_moves, game.n_pushes, game.score,
  161.          obj[game.y][game.x]->power - obj[game.y][game.x]->weight);
  162. }
  163. void cmd_ShowBestScore(void) {
  164.     if (highscore[game.level])
  165.     show_message(TXT_BEST, highscore[game.level+100],
  166.              highscore[game.level+200], highscore[game.level]);
  167.     else
  168.     show_message(TXT_UNSOLVED);
  169. }
  170. void cmd_ReplayGame(void) {
  171.     if (game.n_moves) {
  172.     int rem = game.n_moves;
  173.     cmd_RestartGame();
  174.     sync_and_wait();
  175.     sync_and_wait();
  176.     do {
  177.         cmd_RedoMove();
  178.         sync_and_wait();
  179.         sync_and_wait();
  180.     } while (game.n_moves < rem);
  181.     cmd_ShowScore();
  182.     }
  183. }
  184.  
  185. void cmd_LoadGame(void) {
  186.     char filename[MAXSAVEFILELEN];
  187.     static const char **p, *extensions[] = { "sv", "bs", "mp", "mm", "sav", "sol", NULL };
  188.     for (p = extensions; *p; ++p) {
  189.     sprintf(filename, "%s/%s.%02d.%s", savedir, game.type, game.level, *p);
  190.     if (!access(filename, R_OK)) {
  191.         load_game(filename);
  192.         return;
  193.     }
  194.     }
  195.     show_message(TXT_NOLOAD);
  196. }
  197. void cmd_StartMacro(void) {
  198.     game.macroStart = game.n_moves;
  199.     game.macroEnd = game.n_moves;
  200.     game.macro_x = game.x;
  201.     game.macro_y = game.y;
  202.     show_message(TXT_STARTMACRO);
  203. }
  204. void cmd_EndMacro(void) {
  205.     if (game.macroStart >= 0) {
  206.     game.macroEnd = game.n_moves;
  207.     show_message(TXT_ENDMACRO, game.n_moves-game.macroStart);
  208.     }
  209. }
  210. void cmd_PlayMacro(void) {
  211.     /* show_message("Stard %d, End %d, at (%d,%d)", game.macroStart, game.macroEnd,
  212.        game.macro_x, game.macro_y); */
  213.     if (game.macroStart >= 0) {
  214.     mouse_x = game.macro_x;
  215.     mouse_y = game.macro_y;
  216.     cmd_MouseMove();    /* sets the before_move variable */
  217.     if (game.x == game.macro_x && game.y == game.macro_y) {
  218.         int i, base;
  219.         base = game.n_moves;
  220.         for (i = game.macroStart; i < game.macroEnd;) {
  221.         playermove(movetab[i++]);
  222.         if (game.n_moves != base + i - game.macroStart)
  223.             break;    /* invalid move */
  224.         }
  225.     } else
  226.         show_message(TXT_MACRO_BADPOS);
  227.     }
  228. }
  229.  
  230. void cmd_UndoMove(void) {
  231.     if (game.n_moves) {
  232.     int num = numeric_arg;
  233.     if (!num)
  234.         num = 1;
  235.     if (game.n_moves >= num)
  236.         jumpto_movenr(game.n_moves-num);
  237.     else
  238.         jumpto_movenr(0);
  239.     show_message(TXT_UNDO);
  240.     if (game.n_moves < game.macroStart || game.n_moves < game.macroEnd)
  241.         game.macroStart = -1;
  242.     } else if (game.stored_moves) {
  243.     jumpto_movenr(game.stored_moves);
  244.     show_message(TXT_UNDO);
  245.     } else
  246.     show_message(TXT_NOUNDO);
  247. }
  248. void cmd_RedoMove(void) {
  249.     if (game.n_moves < game.stored_moves) {
  250.     int num = numeric_arg;
  251.     if (!num)
  252.         num = 1;
  253.     if (game.n_moves + num <= game.stored_moves)
  254.         jumpto_movenr(game.n_moves+num);
  255.     else
  256.         jumpto_movenr(game.stored_moves);
  257.     show_message(TXT_REDO);
  258.     } else
  259.     show_message(TXT_NOREDO);
  260. }
  261.